package cn.zyq.baking.service.impl;

import cn.zyq.baking.mapper.CommentMapper;
import cn.zyq.baking.mapper.ContentMapper;
import cn.zyq.baking.pojo.dto.ContentDTO;
import cn.zyq.baking.pojo.entity.Content;
import cn.zyq.baking.pojo.vo.*;
import cn.zyq.baking.service.IContentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.File;
import java.util.Date;
import java.util.List;

@Service
public class ContentServiceImpl implements IContentService {

    @Autowired
    ContentMapper contentMapper;
    public void addNew(ContentDTO contentDTO) {
        Content content=new Content();
        BeanUtils.copyProperties(contentDTO,content);
        if(contentDTO.getId()==null){//如果是新发布的文章(新发布的文章没有id)
            content.setCreateTime(new Date());
            contentMapper.insert(content);
        }else{//如果是修改文章
            content.setUpdateTime(new Date());
            contentMapper.update(content);
        }
    }

    public List<ContentManagementVO> listForManagement(Long type, Long userId) {
        return contentMapper.listForManagement_byTypeAndUserId(type,userId);
    }

    @Autowired
    CommentMapper commentMapper;

    @Value("${file-path}")
    private String dirPath;
    public void deleteById(Long contentId) {
        //先根据id查询要删除的文章(主要需要图片或视频路径)
        ContentDetailVO contentDetailVO=contentMapper.selectDetailById(contentId);
        //删除对应的图片
        new File(dirPath+contentDetailVO.getImgUrl()).delete();
        if(contentDetailVO.getType()==5){//如果是视频
            new File(dirPath+contentDetailVO.getVideoUrl()).delete();
        }
        //通过内容id删除数据库中和此内容相关的评论
        commentMapper.deleteByContentId(contentId);
        //通过内容id删除数据库中的内容数据
        contentMapper.deleteById(contentId);
    }

    public ContentUpdateVO selectForUpdateById(Long id) {
        return contentMapper.selectForUpdateById(id);
    }

    public List<ContentIndexVO> listForIndex_byTypeAndCategoryId(Long type, Long categoryId) {
        return contentMapper.listForIndexByTypeAndCategoryId(type,categoryId);
    }

    public ContentDetailVO selectDetailById(Long contentId) {
        contentMapper.updateViewCountById(contentId);//根据contentId更新浏览量
        return contentMapper.selectDetailById(contentId);
    }

    public List<ContentSimpleVO> otherListByUserId(Long userId) {
        return contentMapper.otherListByUserId(userId);
    }

    public List<ContentSimpleVO> hotList() {
        return contentMapper.listHot();
    }

    public List<ContentIndexVO> listByType(Long type) {
        return contentMapper.listByType(type);
    }

    public List<ContentIndexVO> searchForIndex_byWord(String keyWord) {
        return contentMapper.searchForIndex_byWord(keyWord);
    }

    public List<ContentAdminVO> listForAdminByType(Long type) {
        return contentMapper.listForAdminByType(type);
    }
}
